package com.dynamic.lesscss.engine;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class LesscssAction extends AbstractAction implements IObjectActionDelegate {

	private Shell shell;
	private ISelection selection=null;
	/**
	 * Constructor for Action1.
	 */
	public LesscssAction() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		if(selection instanceof IStructuredSelection){
			//IFile file=(IFile)selection;
			Object element = ((IStructuredSelection)selection).getFirstElement(); 
			if (element instanceof IResource) {
				IResource resource = (IResource) element;
				this.compileFile(resource);
				try {
					resource.getParent().refreshLocal(IResource.DEPTH_INFINITE,null);
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void selectionChanged(IAction arg0, ISelection selection) {
		this.selection=selection;
	}

}
